package com.lmaos.basetools.menuList;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.lmaos.basetools.authority.AuthorityMg;
import com.lmaos.basetools.authority.UserAuthority;
import com.lmaos.basetools.sText.TextUtils;
/**
 * 
 * @author lmaos
 *
 * <p>作者：张星宇</p>
 * <p>时间：2017年6月16日 下午2:27:51</p>
 * <p>说明:菜单权限的工具		</p>
 *
 */
public class MenuAuthorityUtlis {
	public static String menuAuthorityUrlsKey = "menuAuthorityUrlsKey";
	/**
	 * 说明：添加菜单权限记录		<br>
	 * 时间：2017年6月16日 下午4:04:38
	 *
	 * @param authorityMg
	 * @param userRoleKey
	 * @param authorityUrls
	 */
	public static void addMenuAuthorityUrls(AuthorityMg authorityMg,String userRoleKey,Set<String> authorityUrls){
		UserAuthority userAuthority = authorityMg.getUserAuthority(userRoleKey);
		if(userAuthority == null){
			return;
		}
		userAuthority.put(menuAuthorityUrlsKey, authorityUrls);
	}
	/**
	 * 说明：从权限中得到菜单url记录		<br>
	 * 时间：2017年6月16日 下午4:04:04
	 *
	 * @param authorityMg
	 * @param userRoleKey
	 * @return
	 */
	public static Set<String> getMenuAuthorityUrls(AuthorityMg authorityMg,String userRoleKey){
		UserAuthority userAuthority = authorityMg.getUserAuthority(userRoleKey);
		if(userAuthority == null){
			return null;
		}
		return userAuthority.getOBJ(menuAuthorityUrlsKey);
		
	}
	/**
	 * 说明：获得权限存在的菜单项		<br>
	 * 时间：2017年6月16日 下午2:28:25
	 *
	 * @param menuMg
	 * @param path
	 * @param authorityUrls
	 * @return
	 */
	public static Map<String, MenuItem> getMenuItemsAuthority(MenuMg menuMg,String path,Set<String> authorityUrls){
		if(TextUtils.isBlank(path)){
			path = "/";
		}
		MenuDirectory menuDirectory = menuMg.getMenuDirectoryByPath(path);
		if(menuDirectory == null||authorityUrls == null){
			return new HashMap<String, MenuItem>();
		}
		Map<String, MenuItem> menuItems = menuDirectory.getMenuItems();
		Set<Entry<String, MenuItem>> entrys = new HashSet<Entry<String, MenuItem>>(menuItems.entrySet());
		for (Entry<String, MenuItem> entry : entrys) {
			MenuItem menuItem = entry.getValue();
			if(menuItem!=null){
				if(!authorityUrls.contains(menuItem.getAbsoluteMenuPath())){
					menuItems.remove(entry.getKey());
				}
			}
		}
		return menuItems;
	}
	
	public static void main(String[] args) {
		Set<String> authorityUrls = new HashSet<String>();
		authorityUrls.add("/abc");
		authorityUrls.add("/ccc");
		authorityUrls.add("/fff/");
		authorityUrls.add("/fff/bb");
		authorityUrls.add("/fff/aa/");
		MenuMg m = new MenuMgImpl();
		m.addMenuItem(new MenuItem("/abc"));
		m.addMenuItem(new MenuItem("/ccc"));
//		m.addMenuItem(new MenuItem("/fff/bb"));
		m.addMenuItem(new MenuItem("/fff/aa/cc"));
		System.out.println(getMenuItemsAuthority(m, "/fff/", authorityUrls));
	}
}
